//
//  LKLockScanViewController.m
//  LKIntelligentVideoProject
//
//  Created by she1323 on 2018/9/20.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKLockScanViewController.h"

#import <LBXAlertAction.h>


@interface LKLockScanViewController ()


@end

@implementation LKLockScanViewController



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self drawBottomItems];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"扫一扫";
    self.cameraInvokeMsg = @"相机启动中";
    [self setNavgationBackBtn];
}
/**
 设置返回按钮样式
 */
-(void)setNavgationBackBtn{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"mapBack"] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitle:@" 返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.bounds = CGRectMake(0, 0, 70, 30);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)back
{
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count > 1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{
        //present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)drawBottomItems
{
    if (_bottomItemsView) {        
        return;
    }
    self.bottomItemsView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame)-164,
                                                                   CGRectGetWidth(self.view.frame), 100)];
    _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    [self.view addSubview:_bottomItemsView];
    
    CGSize size = CGSizeMake(65, 87);
    self.btnFlash = [[UIButton alloc] init];
    _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
    _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/2, CGRectGetHeight(_bottomItemsView.frame)/2);
    [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
    [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
    [_bottomItemsView addSubview:_btnFlash];
    [_btnFlash mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.bottomItemsView);
    }];
    

}
/**
 开启或者关闭闪光灯
 */
-(void)openOrCloseFlash{
    [super openOrCloseFlash];
    if (self.isOpenFlash)
    {
        [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_down"] forState:UIControlStateNormal];
    }else{
        [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -实现类继承该方法，作出对应处理

- (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
{
    if (!array ||  array.count < 1)
    {
        [self popAlertMsgWithScanResult:nil];
        
        return;
    }
    
    //经测试，可以ZXing同时识别2个二维码，不能同时识别二维码和条形码
    //    for (LBXScanResult *result in array) {
    //
    //        NSLog(@"scanResult:%@",result.strScanned);
    //    }
    
    LBXScanResult *scanResult = array[0];
    
    NSString*strResult = scanResult.strScanned;
    
    self.scanImage = scanResult.imgScanned;
    
    if (!strResult) {
        
        [self popAlertMsgWithScanResult:nil];
        
        return;
    }
    
    //TODO: 这里可以根据需要自行添加震动或播放声音提示相关代码
    //...
    [self showNextVCWithScanResult:scanResult];
}

- (void)popAlertMsgWithScanResult:(NSString*)strResult
{
    if (!strResult) {
        
        strResult = @"识别失败";
    }
    
    __weak __typeof(self) weakSelf = self;
    [LBXAlertAction showAlertWithTitle:@"扫码内容" msg:strResult buttonsStatement:@[@"知道了"] chooseBlock:^(NSInteger buttonIdx) {
        
        [weakSelf reStartDevice];
    }];
}
//扫描结果展示
- (void)showNextVCWithScanResult:(LBXScanResult*)strResult {
    

    
    NSLog(@"二维码为：%@",strResult.strScanned);
<<<<<<< HEAD
    NSInteger scanValue = [strResult.strScanned integerValue];
    NSInteger secretValue = scanValue + 50;
    NSString *secretValueStr = [NSString stringWithFormat:@"%ld",secretValue];
    NSDictionary *param = @{@"sweepUser":[LKUserInforTool account].loginAccount,@"sweepPwd":secretValueStr};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerUrl apiPath:@"/sweep/server/app/addSweepLoggerRecord" parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];

    WS(weakSelf)
    NSMutableAttributedString *attribuStr = [[NSMutableAttributedString alloc] initWithString:secretValueStr];
    [attribuStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, secretValueStr.length)];

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开锁密码" message:secretValueStr preferredStyle:UIAlertControllerStyleAlert];
    [alert setValue:attribuStr forKey:@"attributedMessage"];
=======
    WS(weakSelf)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开锁密码" message:strResult.strScanned preferredStyle:UIAlertControllerStyleAlert];
    
>>>>>>> parent of 2e14138... 提交
    [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf reStartDevice];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
